import { queryProfile, queryAdvancedProfile, updateAdvancedProfile } from '../services/api';

export default {
  namespace: 'profile',

  state: {
    userName: '',
    orgName: '',
    fullName: '',
    roleName: '',
    mobPhone: '',
    email: '',
    fixPhone: '',
    fax: '',
    hpPicUrl: '',
  },

  effects: {
    *fetch(_, { call, put }) {
      const response = yield call(queryProfile);

      yield put({
        type: 'save',
        payload: response.amUserInfo,
      });
    },
    *fetchAdvanced(_, { call, put }) {
      const response = yield call(queryAdvancedProfile);
      yield put({
        type: 'show',
        payload: response,
      });
    },
    *updateAdvanced({ payload }, { call, put }) {
      const response = yield call(updateAdvancedProfile, payload);
      yield put({
        type: 'show',
        payload: response,
      });
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    show(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
